package cn.stronglink.assetmanage.business.inventory.view.uncount;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cn.stronglink.assetmanage.MyApplication;
import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.business.inventory.adapter.InventoryListAdapter;
import cn.stronglink.assetmanage.business.inventory.adapter.InventoryOrderAdapter;
import cn.stronglink.assetmanage.business.inventory.model.InventoryModel;
import cn.stronglink.assetmanage.business.inventory.model.InventoryUncountModel;
import cn.stronglink.assetmanage.business.inventory.presenter.InventoryUncountPresenter;
import cn.stronglink.assetmanage.business.inventory.view.IRefreshList;
import cn.stronglink.assetmanage.dao.DaoManager;
import cn.stronglink.assetmanage.dao.SyncTypesDao;
import cn.stronglink.assetmanage.entity.Inventory;
import cn.stronglink.assetmanage.entity.SyncTypes;
import cn.stronglink.assetmanage.index.base.BaseFragment;
import cn.stronglink.assetmanage.index.base.BasePresenter;
import cn.stronglink.assetmanage.index.presenter.MenuPresenter;
import cn.stronglink.assetmanage.index.view.onRecyclerListener;
import cn.stronglink.assetmanage.utils.Constants;
import cn.stronglink.assetmanage.utils.ToastUtil;
import cn.stronglink.assetmanage.widget.CustomProgressDialog;
import cn.stronglink.assetmanage.widget.ViewPagerFragment;
import okhttp3.Call;
import okhttp3.Response;

import static android.R.attr.fragment;

/**
 * 未盘点列表
 */
public class InventoryUncountFragment extends ViewPagerFragment implements InventoryModel.onInventoryDataListener, IRefreshList {
    private RecyclerView rv_inventoryuncount_order_list;
    private InventoryListAdapter listAdapter;
    private InventoryUncountModel inventoryUncountModel;
    private InventoryUncountView inventoryUncountView;
    private InventoryUncountPresenter inventoryUncountPresenter;
    private LinearLayoutManager layoutmanager;

    private TextView txt_main_title;
    private SwipeRefreshLayout refresh_inventory_list;
    private InventoryModel mInventoryModel;
    protected Activity mActivity;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_inventory_uncount, container, false);
        try {
            initViewAndData(rootView);
        } catch (Exception e) {
            if (inventoryUncountView != null && inventoryUncountView.getFragment() != null) {
                Toast.makeText(inventoryUncountView.getFragment().getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }

        return rootView;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mActivity = (Activity) context;
    }

    private void initViewAndData(View view) {
        txt_main_title = this.getActivity().findViewById(R.id.txt_main_title);
        txt_main_title.setText("盘点管理");
        rv_inventoryuncount_order_list = view.findViewById(R.id.rv_inventoryuncount_order_list);
        listAdapter = new InventoryListAdapter(getActivity(), null);
        layoutmanager = new LinearLayoutManager(this.getActivity(), LinearLayoutManager.VERTICAL, false);
        rv_inventoryuncount_order_list.setLayoutManager(layoutmanager);
        rv_inventoryuncount_order_list.setAdapter(listAdapter);
        listAdapter.setOnItemClickListener(new onRecyclerListener() {
            @Override
            public void onItemClicked(View v, int postion) {
                inventoryUncountPresenter.showOrderByPosition(postion);
            }
        });

        inventoryUncountModel = new InventoryUncountModel();
        inventoryUncountView = new InventoryUncountView(this, view, listAdapter);
        inventoryUncountPresenter = new InventoryUncountPresenter(inventoryUncountModel, inventoryUncountView);
        refresh_inventory_list = view.findViewById(R.id.refresh_inventory_list);
        refresh_inventory_list.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pullToInventoryData();
            }
        });
        mInventoryModel = new InventoryModel();
        this.refreshList();
    }


    @Override
    public void refreshList() {
        refreshData();
    }

    public void pullToInventoryData() {
        SyncTypes syncTypes = DaoManager.getDaoSession(this.getActivity()).getSyncTypesDao().
                queryBuilder().where(SyncTypesDao.Properties.IsSelected.eq(true)).unique();
        int tp = 0;
        if (syncTypes != null) {
            tp = syncTypes.getSyscTs();
        }
        switch (tp) {
            case Constants.STATUS_ONE:
                //自动
                mInventoryModel.getInventoryData();
                mInventoryModel.setOnInventoryDataListener(this);
                break;
            case Constants.STATUS_TWO:
                //离线，从数据库拿数据
                if (refresh_inventory_list != null) {
                    refresh_inventory_list.setRefreshing(false);
                }
                ToastUtil.toastShort("本地数据加载完成");
                break;
        }
    }


    private void refreshData() {
        if (inventoryUncountPresenter == null) return;
        inventoryUncountPresenter.initInventoryListView();
    }

    @Override
    public void onFailure(Call call, IOException ex) {
        if (mActivity == null) {
            return;
        } else {
            mActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (refresh_inventory_list != null) {
                        refresh_inventory_list.setRefreshing(false);
                    }
                }
            });
        }
    }

    @Override
    public void onResponse(Call call, Response response) {
        if (mActivity == null) {
            return;
        } else {
            mActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (refresh_inventory_list != null) {
                        refresh_inventory_list.setRefreshing(false);
                    }
                }
            });
        }
        try {
            //取消progress提示框
            if (response.isSuccessful()) {
                if (response.code() == 200) {
                    String json = response.body().string();
                    if (json != null && !"".equals(json)) {
                        mInventoryModel.saveInventoryData(json);
                        ToastUtil.toastShort("更新完成");
                        mActivity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                refreshData();
                            }
                        });

                    } else {
                        ToastUtil.toastShort(MyApplication.APP_CONTEXT.getString(R.string.get_no_data));
                    }
                } else {
                    ToastUtil.toastShort(MyApplication.APP_CONTEXT.getString(R.string.get_data_fail));
                }
            } else {
                ToastUtil.toastShort(MyApplication.APP_CONTEXT.getString(R.string.get_data_fail));
            }
        } catch (Exception e) {
            ToastUtil.toastShort(e.getMessage());
        }
    }
}
